package com.manager.devicemanager.service.impl;

import com.manager.devicemanager.entity.Statusaccounting;
import com.manager.devicemanager.mapper.StatusaccountingMapper;
import com.manager.devicemanager.service.IStatusaccountingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wpf
 * @since 2025-06-24
 */
@Service
public class StatusaccountingServiceImpl extends ServiceImpl<StatusaccountingMapper, Statusaccounting> implements IStatusaccountingService {

    @Autowired
    private StatusaccountingMapper statusaccountingMapper;

    @Override
    public Statusaccounting getDataLast(String dataSourceName) {
        return statusaccountingMapper.getDataLastIc();
    }


    @Override
    public List<Statusaccounting> getLastWeek(String dataSourceName) {
        List<Statusaccounting> resultList = new ArrayList<>();
        // 获取当前日期
        LocalDate endDate = LocalDate.now();
        // 获取一周前的日期
        LocalDate startDate = endDate.minusDays(6);

        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {

            // 将 LocalDate 转换为 java.util.Date
            Date targetDate = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
            Statusaccounting lastData = null;
            try {
                lastData = statusaccountingMapper.getLastDataByDayIC(targetDate);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("获取天" + lastData + "数据失败");
            }
            if (lastData == null) {
                lastData = new Statusaccounting();
                lastData.setTime(targetDate);
                lastData.setStatusCnt1(0);
                lastData.setStatusCnt2(0);
                lastData.setStatusCnt3(0);
                lastData.setStatusCnt4(0);
            }

            resultList.add(lastData);


        }
        return resultList;
    }

    @Override
    public List<Statusaccounting> getLastSixMonths(String dataSourceName) {
        List<Statusaccounting> resultList = new ArrayList<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();


        for (int i = 0; i < 6; i++) {
            // 其他月份取当月最后一天数据
            LocalDate   targetLocalDate = today.withDayOfMonth(1).minusMonths(i);
            // 生成年月字符串（格式：YYYY-MM）
            String yearMonthStr = targetLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            Statusaccounting lastData = null;
            try {
                lastData = statusaccountingMapper.getMonthLastDataByDayIC(yearMonthStr);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("获取月" + yearMonthStr + "数据失败");
            }
            if (lastData == null) {
                lastData = new Statusaccounting();
                lastData.setTime(Date.from(targetLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
                lastData.setStatusCnt1(0);
                lastData.setStatusCnt2(0);
                lastData.setStatusCnt3(0);
                lastData.setStatusCnt4(0);
            }

            resultList.add(lastData);
        }

        // 反转列表，使数据按时间顺序从早到晚排列
        Collections.reverse(resultList);

        return resultList;
    }

}
